package com.theme.passport.service.theme.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.utils.Query;
import com.theme.passport.dao.theme.RecommendProductDao;
import com.theme.passport.entity.theme.RecommendProductEntity;
import com.theme.passport.service.theme.RecommendProductService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("recommendProductService")
public class RecommendProductServiceImpl extends ServiceImpl<RecommendProductDao, RecommendProductEntity> implements RecommendProductService {

    @Autowired
    private RecommendProductService themeRecommendService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String status = (String) params.get("status");
        String type = (String) params.get("type");
        String recommendId = (String) params.get("recommendId");
        IPage<RecommendProductEntity> page = this.page(
                new Query<RecommendProductEntity>().getPage(params),
                new QueryWrapper<RecommendProductEntity>()
                        .eq(StringUtils.isNotBlank(type), "type", type)
                        .eq(StringUtils.isNotBlank(recommendId), "recommend_id", recommendId)
                        .in(StringUtils.isNotBlank(status), "status", status)
                        .orderByDesc("created")
        );
        return new PageUtils(page);
    }

    @Override
    public void updateRecommendProduct(List<Long> objectIds, Integer type, Long recommendId) {
        //1、删除以及存在的
        Wrapper<RecommendProductEntity> queryWrapper = new QueryWrapper<RecommendProductEntity>()
                .eq("recommend_id", recommendId);
        List<RecommendProductEntity> alreadyList = themeRecommendService.list(queryWrapper);

        List<Long> recommendIds = new ArrayList<>(10);
        alreadyList.forEach(item ->{
            recommendIds.add(item.getId());
        });
        themeRecommendService.removeByIds(recommendIds);
        //2、新增新的列表
        List<RecommendProductEntity> newList = new ArrayList<>(10);
        objectIds.forEach(objectId ->{
            RecommendProductEntity recommend = new RecommendProductEntity();
            recommend.setType(type);
            recommend.setRecommendId(recommendId);
            recommend.setObjectId(objectId);
            recommend.setCreated(new Date());
            recommend.setUpdated(new Date());
            newList.add(recommend);
        });
        themeRecommendService.saveBatch(newList);
    }
}